<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
    <title>选择体检项目</title>
    <base href="<%=basePath %>"/>
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
    <%--<link rel="stylesheet" type="text/css" href="assets/model/css/normalize.css" />--%>
    <%--<link rel="stylesheet" type="text/css" href="assets/model/css/demo.css">--%>
    <%--<link rel="stylesheet" href="assets/model/css/style.css">--%>
    <%--<link rel="stylesheet" href="assets/model/dist/MODALit.min.css">--%>
    <link rel="stylesheet" href="assets/css/zTreeStyle/zTreeStyle.css" type="text/css">
    <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.css"/>
    <!--复选框样式-->
    <link rel="stylesheet" type="text/css" href="assets/bootstrap-checkbox/css/awesome-bootstrap-checkbox.css"/>
    <!--复选框字体样式-->
    <link rel="stylesheet" href="assets/bootstrap-checkbox/Font-Awesome/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="assets/bootstrap-checkbox/css/build.css"/>

    <script src="assets/js/jquery-3.3.1.js"></script>
    <script src="assets/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="assets/js/jquery.ztree.core.js"></script>
    <script type="text/javascript" src="assets/js/jquery.ztree.excheck.js"></script>
    <%--<script src="assets/model/dist/MODALit.min.js"></script>--%>
    <%--<script src="assets/model/js/script.js"></script>--%>

</head>
<body style="background:url(assets/layui/font/bg-1.png)">
<div class="panel-body"></div>
<div class="container" style="text-align: center">
    <div class="btn-group" role="group" aria-label="btn-group">
        <a class="btn btn-default" id="comboChoiceBtn" onclick="itemChoice(0)">选择套餐</a>
        <a class="btn btn-primary" id="itemChoiceBtn" onclick="itemChoice(1)">自选项目</a>
    </div>
    <div id="comboChoiceDiv">选择套餐</div>
    <div id="itemChoiceDiv" hidden>
        <form id='itemListForm'>
            <div>
                <div class="col-lg-4 col-lg-offset-4" id="treeDemo" style="text-align: left">

                </div>
            </div>
        </form>
    </div>
</div>
</body>
<script>
    //初始化
    $(document).ready(function () {
        comboSelect(1);
        getDesks();
    })
    //项目选取类型
    function itemChoice(num) {
        if (num==0&&$("#comboChoiceDiv").is(":hidden")){
               $("#comboChoiceBtn").toggleClass('btn-default btn-primary');
               $("#itemChoiceBtn").toggleClass('btn-default btn-primary');
               $("#comboChoiceDiv").show();//显示套餐选择div
               $("#itemChoiceDiv").hide();//隐藏项目选择div
        } else if (num==1&&$("#itemChoiceDiv").is(":hidden")){
            $("#comboChoiceBtn").toggleClass('btn-default btn-primary');
            $("#itemChoiceBtn").toggleClass('btn-default btn-primary');
            $("#comboChoiceDiv").hide();//隐藏套餐选择div
            $("#itemChoiceDiv").show();//显示项目选择div
        }
    }

    //查找套餐列表
    function comboSelect(pageNum) {
        if (pageNum > 0) {
            $.post('combo/getCombos.action?pageNum=' + pageNum,
                $('#selectComdo').serialize(),
                function (combos) {
                console.log(combos);
                    if (combos.list.length == 0) {
                        $('#comboChoiceDiv').html(null);
                        $('#comboChoiceDiv').append("没有符合条件的数据");
                    } else {
                        $('#comboChoiceDiv').html(null);
                        $('#comboChoiceDiv').append("<br>");
                        combos.list.forEach(function (v) {
                            var str = "<div class='panel panel-default'>"+
                                "<div class='panel-heading' data-toggle='collapse' data-parent='#accordion' href='#"+v.comboId+"' onclick='getCombo("+v.comboId+")'>"+
                                "<h4 class='panel-title'>"+
                                v.comboName + "</h4></div>"+
                                "<div id='"+v.comboId+"' class='panel-collapse collapse'>"+
                                "</div></div>";
                            $('#comboChoiceDiv').append(str);
                        });
                        $('#comboChoiceDiv').append(
                            "<a class='btn btn-primary' onclick='comboSelect("+combos.prePage+")'>上一页</a> "+
                            "<a class='btn btn-primary' onclick='comboSelect("+combos.nextPage+")'>下一页</a>"
                        );
                    }
                }
            )
        }
    }

    //参看套餐详情
    function getCombo(comboId) {
        $.post('combo/getCombo.action?comboId=' + comboId,
            function (combo) {
                $('#'+comboId).html(null);
                if (combo.items.length == 0) {
                    $('#'+comboId).append("没有项目或项目已删除");
                } else {
                    var str = "<div class='panel-body'><p>价格："+combo.comboMoney+"</p>";
                    combo.items.forEach(function (v) {
                        str += "<p>"+v.itemName+"</p>";
                        }
                    )
                    str += "<a class='btn-primary btn-sm' onclick='addReportCombo("+comboId+")'>选择</a></div>";
                    $('#'+comboId).append(str);
                }
            })
    }

    //根据套餐增加报告
    function addReportCombo(comboId) {
        $.post('report/phAddReportCombo.action',
            {'comboId':comboId},
            function (str) {
                console.log(str);
                switch (str.toString()) {
                    case"true":
                        alert("套餐选择成功！");
                        break;
                    case"false":
                        alert("套餐选择失败！");
                        break;
                    case"fundShortage":
                        alert("用户余额不足！");
                        location.href="userTransaction/showUserRecharge.action";
                        break;
                }
            }
        ),"text"
    }

    //查询科室列表
    function getDesks() {
        $('#itemList').html(null);
        $('#itemListDiv').html(null);
        $.post('desk/getDesk.action',
            function (data) {
                var str = "";
                data.forEach(function (desk) {
                    str+= "<h3>"+desk.deskName+"</h3>";
                    desk.items.forEach(function (item) {
                        str+="<div class='checkbox checkbox-primary'>"+
                             "<input id='itemId"+item.itemId+"' value='"+item.itemId+"' class='styled' type='checkbox'>"+
                             "<label for='itemId"+item.itemId+"'>"+item.itemName+"</label></div>"
                    })
                })
                str+="<a class='btn-primary btn-sm' onclick='addReportItem()'>提交</a>"
                $('#treeDemo').append(str);
            }
        )
    }

    //自选项目添加报告
    function addReportItem() {
        //var checkedObjs = $.fn.zTree.getZTreeObj("treeDemo").getCheckedNodes(true);
        //将选中的值放到input中传到后台
        // $("#itemListForm input[name='itemIds']").remove();
        // for(var i=0;i<checkedObjs.length;i++) {
        //     var obj = checkedObjs[i];
        //     if (obj.pId!=null) {
        //         $("#itemListForm").append("<input type='hidden' name='itemIds' value='"+obj.id+"' >");  //在form里增加input，将字符串数组传到后台
        //     }
        // }

        var itemIds = new Array();
        $(":checked").each(function (i) {
            itemIds[i] = $(this).val();
        })
        console.log($('#itemListForm').serialize());
        $.post('report/phAddReportItem.action',
            {"itemIds":itemIds.toString()},
            function (str) {
                switch (str.toString()) {
                    case"true":
                        alert("选择成功！");
                        break;
                    case"false":
                        alert("选择失败！");
                        break;
                }
            }),"text";
    }

    //树形参数
    var setting = {

        check: {
            enable: true,
            nocheckInherit: true
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    };
    function nocheckNode(e) {
        var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
            nocheck = e.data.nocheck,
            nodes = zTree.getSelectedNodes();
        if (nodes.length == 0) {
            alert("请先选择一个节点");
        }

        for (var i=0, l=nodes.length; i<l; i++) {
            nodes[i].nocheck = nocheck;
            zTree.updateNode(nodes[i]);
        }
    }
</script>
</html>